import logging
from typing import Optional, List, Type

from gpt.model.adapter.base import get_model_adapter
from gpt.model.adapter.llm_adapter import LLMModelAdapter
from gpt.model.base import ModelType
from gpt.model.parameters import BaseModelParameters, EmbeddingModelParameters, WorkerType

logger = logging.getLogger(__name__)


def get_llm_model_adapter(
        model_name: str,
        model_path: str,
        model_type: str = None,
) -> LLMModelAdapter:
    new_model_adapter = get_model_adapter(
        model_type, model_name, model_path
    )
    if new_model_adapter:
        logger.info(f"Current model {model_name} use new adapter {new_model_adapter}")
        return new_model_adapter


def _dynamic_model_parser() -> Optional[List[Type[BaseModelParameters]]]:
    """Dynamic model parser, parse the model parameters from the command line arguments.

    Returns:
        Optional[List[Type[BaseModelParameters]]]: The model parameters class list.
    """
    from gpt.util.parameter_utils import _SimpleArgParser

    pre_args = _SimpleArgParser("model_name", "model_path", "worker_type", "model_type")
    pre_args.parse()
    model_name = pre_args.get("model_name")
    model_path = pre_args.get("model_path")
    worker_type = pre_args.get("worker_type")
    model_type = pre_args.get("model_type")
    if worker_type == WorkerType.TEXT2VEC:
        return [
            EMBEDDING_NAME_TO_PARAMETER_CLASS_CONFIG.get(
                model_name, EmbeddingModelParameters
            )
        ]
    if model_name is None and model_type != ModelType.VLLM:
        return None
    llm_adapter = get_llm_model_adapter(model_name, model_path, model_type=model_type)
    param_class = llm_adapter.model_param_class()
    return [param_class]